from django import  forms
from .models import *
import re
#自定义的表单类
class RegisterForm(forms.Form):
    account = forms.EmailField(max_length=20,
                               error_messages={'max_length':'长度不能超过20','required':'用户名不能为空'})
    password = forms.CharField(min_length=32,max_length=32,
                               error_messages={'max_length':'长度不能超过6','min_length':'长度不能小于6','required':'密码不能为空'})

    #自定义的校验方法
    def clean(self):
        forms.Form.clean(self)
        email = self.cleaned_data.get('account','')
        if email:
            if not re.match(r'^[a-zA-Z0-9_-]{6,}@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$',email):
                self.errors['account'] = '不能一个有效的邮件地址'
            elif User.is_exist(email):
                self.errors['account'] = '用户已经存在'
